package com.htjc.uep.center.basic.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.htjc.uep.center.basic.service.IBasicService;
import com.htjc.uep.center.basic.service.IConsumeService;
import com.htjc.uep.center.commons.annotation.Decrypt;
import com.htjc.uep.center.commons.utils.JsonUtil;
import com.htjc.uep.center.commons.utils.StringUtil;
import com.htjc.uep.center.model.basic.request.BasicApplyRequest;
import com.htjc.uep.center.model.basic.request.ConsumePoolRequest;
import com.htjc.uep.center.model.enums.ErrorStatus;

@RequestMapping("/jf_basic/basic")
@RestController
public class BasicController {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IBasicService basicService;
	
	@Autowired
	private IConsumeService consumeService;

	/**
	 * 合作平台申请
	 * @param str
	 * @return
	 */
	@Decrypt
	@RequestMapping(value = {"applyPlatform"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String applyPlatform(@RequestBody String str) {
		
		try {
			log.info("积分中心-业务申请接口-入参={}", str);
			
			BasicApplyRequest applyRequest = JsonUtil.json2Bean(str, BasicApplyRequest.class);
			if (StringUtil.isEmpty(applyRequest.getBusinessId(), applyRequest.getMechanismCode(), applyRequest.getOperator(),
					applyRequest.getOperatorNum().toString(), applyRequest.getOperatorType(), applyRequest.getRepertoryId(), 
					applyRequest.getRepertoryName(), applyRequest.getSourceNo())) {
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			log.info("积分中心-业务申请接口-申请类型={}", applyRequest.getOperatorType());
			return basicService.addPlatform(applyRequest);
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}
	}
	/**
	 *积分发行申请
	 * @param str
	 * @return
	 */
	@Decrypt
	@RequestMapping(value = {"applyAddPool"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String applyAddPool(@RequestBody String str) {
		
		try {
			log.info("积分中心-业务申请接口-入参={}", str);
			
			BasicApplyRequest applyRequest = JsonUtil.json2Bean(str, BasicApplyRequest.class);
			if (StringUtil.isEmpty(applyRequest.getBusinessId(), applyRequest.getMechanismCode(), applyRequest.getOperator(),
					applyRequest.getOperatorNum().toString(), applyRequest.getOperatorType(), applyRequest.getRepertoryId(), 
					applyRequest.getRepertoryName(), applyRequest.getSourceNo())) {
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			log.info("积分中心-业务申请接口-申请类型={}", applyRequest.getOperatorType());
			return basicService.addPoolApply(applyRequest);
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}
	}
	/**
	 * 积分池扣减接口
	 * @param str
	 * @return
	 */
	@Decrypt
	@RequestMapping(value = {"consumePool"},produces = {"text/json;charset=UTF-8", "application/json;"})
	public String consumePool(@RequestBody String str) {
		String result = "";
		
		try {
			log.info("积分中心-积分池扣减-入参={}", str);
			ConsumePoolRequest consumeRequest = JsonUtil.json2Bean(str, ConsumePoolRequest.class);
			
			String accountCode = consumeRequest.getAccountCode();
			String platform = consumeRequest.getMechanismCode();
			
			if (StringUtil.isEmpty(consumeRequest.getAccountCode(),consumeRequest.getDealId(), consumeRequest.getMechanismCode(), consumeRequest.getScore().toString())) {
				log.info("积分中心-积分池扣减-部分参数为空");
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			result = consumeService.consumePool(consumeRequest);
			log.info("积分中心-积分池扣减完成-合作平台编码={},结算单位编码={}",platform, accountCode);
			return result;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}
	}
}





















